<?php
/**
 * Description: 获取每日一题全局设置
 * User: devforma
 * Date: 16/3/3
 * Time: 16:53
 */

class GetGlobalSettings implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {

        $data = array();

        //登录注册客服QQ号
        $serviceQQ = getOption('service_qq');
        $serviceQQ = empty($serviceQQ) ? '' : $serviceQQ;
        $data['service_qq'] = $serviceQQ;

        //review版本
        $isReview = version_cmp($queryArgs['app_version'], getOption('review_version'));
        $data['is_review'] = $isReview != -1;

        //获取新版本更新信息
        $queryArgs['app_version'] = !empty($queryArgs['app_version']) ? $queryArgs['app_version'] : '0.0';
        $newVersion = $this->getNewVersion($queryArgs['terminal_type'], $queryArgs['app_version']);
        $data['new_version'] = $newVersion;


        //每日一题分享url
        $data['topic_share_url'] = YG_MOBILE_URL . '/appShare/index.html#/appShare/dl?id=';

        //时政分享url
        $data['politic_share_url'] = YG_MOBILE_URL . '/appShare/index.html#/appShare/hp?id=';

        //每日一面分享url
        $data['interview_share_url'] = YG_MOBILE_URL . '/appShare/index.html#/appShare/dailyInterview?id=';

        //app分享下载地址
        $data['app_ios_url'] = 'http://t.cn/Rqx5YO7';
        $data['app_android_url'] = 'http://t.cn/Rqx8tyo';

        //课程分享地址
        $data['course_share_url'] = YG_MOBILE_URL . '/terminalType/shareCourse.html?course_id=';

        //面试购买价格
        $data['interview_id'] = 1128;

        //获取待发布的每日一题和时政
        $data['coming_topics'] = $this->getComingTopics();
        $data['coming_politics'] = $this->getComingPolitics();

        //公开课营销QQ
        $marketQQ = getOption('open_course_market_qq');
        $data['market_qq'] = empty($marketQQ) ? '' : $marketQQ;

        //最新的通知
        $data['latest_notify'] = $this->getLatestNotify();

        //评价文字标签
        $tags = array();
        $starTags = getOption('star_tags');
        if (!empty($starTags) && is_array($starTags)) {
            foreach ($starTags as $index => $tag) {
                $tags[] = array(
                    'star' => intval($index),
                    'tags' => explode(',', $tag)
                );
            }
        }
        $data['rate_tags'] = $tags;


        //微信广告数据追踪
        if (!empty($queryArgs['idfa'])){
            youqianIdfa('dailylearn', $queryArgs['idfa']);
            weixinIdfa('dailylearn', $queryArgs['idfa']);
            pandaPlayIdfa('dailylearn',$queryArgs['idfa']);
        }

        return genOutput(1, $data);
    }

    /**
     * 版本更新配置
     *
     * @param string $terminalType 设备类型
     * @param string $appVersion app版本号
     * @return array|stdClass
     */
    private function getNewVersion($terminalType, $appVersion) {
        $upgradeLogs = getAppUpgradeLog('dailylearn', $terminalType === 'android_phone' ? 'Android' : 'iOS', $appVersion);

        if (!empty($upgradeLogs))
            return array(
                'app_version'  => $upgradeLogs->app_version,
                'size'         => $upgradeLogs->package_size,
                'notice_text'  => $upgradeLogs->content,
                'force_update' => (bool)$upgradeLogs->force_upgrade,
                'target_url'   => !empty($upgradeLogs->package_url) ? $upgradeLogs->package_url : ''
            );

        return new stdClass();
    }

    /**
     * 获取待发布的每日一题
     *
     * @return array
     */
    private function getComingTopics() {
        global $cmsdb;

        $cacheKey = 'func:getComingTopics';

        if (($list = cacheGet($cacheKey)) == null) {

            $sql = 'SELECT `ID`, `post_title` FROM `wp_posts` 
            WHERE post_parent=0 AND post_status="draft" AND post_type="page" ORDER BY ID ASC LIMIT 7';

            $results = (array)$cmsdb->getResults($sql);

            $list = array();
            foreach ($results as $r) {
                $list[] = array(
                    'id' => intval($r->ID),
                    'title' => trim(strtok($r->post_title, '-'))
                );
            }

            cacheAdd($cacheKey, $list, 2*60*60);
        }

        return $list;
    }

    /**
     * 获取待发布的时政热点
     *
     * @return array
     */
    private function getComingPolitics() {
        global $cmsdb;

        $cacheKey = 'func:getComingPolitics';

        if (($list = cacheGet($cacheKey)) == null) {

            $now = date('Y-m-d');
            $sql = "SELECT `id`, `politic_date`, `politic_name` FROM `data_politics` 
            WHERE `publish_status`='draft' AND `has_comment`=1 AND `politic_date`>='{$now}' ORDER BY `id` ASC LIMIT 7";

            $results = (array)$cmsdb->getResults($sql);

            $list = array();
            foreach ($results as $r) {
                $list[] = array(
                    'id' => intval($r->id),
                    'title' => trim(html_entity_decode($r->politic_name)),
                    'date' => $r->politic_date
                );
            }

            cacheAdd($cacheKey, $list, 2*60*60);
        }

        return $list;
    }

    /**
     * 获取最新的通知id，用于app端小红点的展示
     */
    private function getLatestNotify() {
        global $cmsdb;

        $cacheKey = 'func:getLatestNotify:dailylearn';
        if (($value = cacheGet($cacheKey)) == null) {
            $sql = 'SELECT `id` FROM `qb_notify` WHERE `app_type`="dailylearn" ORDER BY `id` DESC LIMIT 1';
            $value = $cmsdb->getVar($sql);

            cacheAdd($cacheKey, $value, 10*60);
        }

        return intval($value);
    }
}